package com.saifan.wyy_ov.ui.communitycenter;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.exception.DbException;
import com.saifan.wyy_ov.R;
import com.saifan.wyy_ov.config.Api;
import com.saifan.wyy_ov.data.bean.RoomInfoBean;
import com.saifan.wyy_ov.model.impl.GetData;
import com.saifan.wyy_ov.model.interfaces.ResponseData;
import com.saifan.wyy_ov.ui.home.MainActivity;
import com.saifan.wyy_ov.ui.set.Project;
import com.saifan.wyy_ov.ui.set.ServerSetActivity;

import com.saifan.wyy_ov.ui.view.BaseActivity;
import com.saifan.wyy_ov.utils.ActivitysManager;


import java.util.List;

import lomasky.ma.utils.SharedPrefUtil;

/**
 * @version V1.0.0
 * @author: lomasky
 * @date: 2015-5-22 15:10:29
 * @descrption
 */

public class RoomSelectActivity extends BaseActivity implements AdapterView.OnItemClickListener {
    private Toolbar toolbar;
    private ListView listView;
    List<RoomInfoBean> roomInfoBeans;
    private MyAdapter adapter;
    private boolean must;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        must = getIntent().getBooleanExtra("must", false);
        setContentView(R.layout.activity_room_select);
        this.listView = (ListView) findViewById(R.id.listView);
        this.toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        String roomInfosString = SharedPrefUtil.getString(this, "roomInfos", "");
        if (!TextUtils.isEmpty(roomInfosString)) {
            roomInfoBeans = new Gson().fromJson(roomInfosString, new TypeToken<List<RoomInfoBean>>() {
            }.getType());

            initAdapter();
        }


    }


    public void initAdapter() {
        adapter = new MyAdapter();
        if (roomInfoBeans != null) {
            listView.setAdapter(adapter);
            adapter.notifyDataSetChanged();
        }
        listView.setOnItemClickListener(this);
    }


    public class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return roomInfoBeans.size();
        }

        @Override
        public Object getItem(int position) {
            return roomInfoBeans.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder;
            if (convertView == null) {
                viewHolder = new ViewHolder();
                convertView = getLayoutInflater().inflate(R.layout.room_select_item, null);
                viewHolder.room = (TextView) convertView.findViewById(R.id.room);
                viewHolder.index = (TextView) convertView.findViewById(R.id.index);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            String room = roomInfoBeans.get(position).getXMDA_XMMC() + "" + roomInfoBeans.get(position).getWYFJDA_FJMC();
            if (roomInfoBeans.get(position).getUserType() == 2 || roomInfoBeans.get(position).getUserType() == 3) {
                room = roomInfoBeans.get(position).getXMDA_XMMC();
            }

            viewHolder.room.setText(room);
            viewHolder.index.setText(position + 1 + "");
            return convertView;

        }

        class ViewHolder {
            android.widget.TextView index;
            android.widget.TextView room;

        }
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


        SharedPrefUtil.putString(this, "WYFJDA_FJMC", roomInfoBeans.get(position).getWYFJDA_FJMC());
        SharedPrefUtil.putString(this, "roomInfo", new Gson().toJson(roomInfoBeans.get(position)));
        setRoomInfoBean(roomInfoBeans.get(position));

        if (roomInfoBeans.get(position).getUserType() == 2) {

            (new GetData()).getDataJson(RoomSelectActivity.this, Api.Customer.TOURISTPERMISSIONSLIST, roomInfoBeans.get(position), "获取权限...", new
                    ResponseData
                            () {
                        @Override
                        public void onSuccess(String json) {
                            SharedPrefUtil.putString(RoomSelectActivity.this, "permissions", json);
                            Intent intent = new Intent(RoomSelectActivity.this, MainActivity.class);
                            startActivity(intent);

                        }

                        @Override
                        public void onFailed(String str) {

                        }
                    });
            return;
        }


        Intent intent = new Intent(RoomSelectActivity.this, MainActivity.class);
        startActivity(intent);

    }


    @Override
    public void onBackPressed() {
        super.onBackPressed();

        if (!must) {
            finish();
        } else {
            Toast.makeText(this, "请选择房间", Toast.LENGTH_LONG).show();
        }


    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();
        if (id == android.R.id.home) {
            if (!must) {
                finish();
            }


        }
        return super.onOptionsItemSelected(item);
    }
}
